package com.wlxk.crawler.core.manager.siluke;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * @description:
 * @author: 21054523
 * @time: 2023/10/24 16:17
 */
public class SilukePageProcessor implements PageProcessor {

    private Site site = Site.me().addCookie("cf_clearance", "5xsifNKNhz24lUml5Lp1MoUBzWQM9USmo6vLuRUzvDQ-1698126346-0-1-a1e6a1d8.93a7734c.700b3f6c-160.2.1698126329").setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    @Override
    public void process(Page page) {
        page.putField("url", page.getUrl().toString());
        List<String> all = page.getHtml().xpath("//a[@class='chapter-all']").links().all();
        if (!CollectionUtils.isEmpty(all)) {
            page.putField("pageType", "home");
            page.putField("title", page.getHtml().xpath("//div[@class='block_txt2']/h2/text()").toString());
            page.putField("author", page.getHtml().xpath("//div[@class='block_txt2']/p/span/text()").toString());
            String[] split = page.getUrl().toString().split("/");
            page.putField("articleId", split[split.length - 1]);
            page.addTargetRequests(all);
            return;
        }
        String zddb = page.getHtml().xpath("//div[@id='chapterlist']/a/text()").toString();
        if (!StringUtils.isEmpty(zddb)) {
            page.putField("pageType", "list");
            String[] split = page.getUrl().toString().split("/");
            page.putField("articleId", split[split.length - 2]);
            List<String> listAll = page.getHtml().xpath("//div[@id='chapterlist']/li").links().all();
            List<String> listTextAll = page.getHtml().xpath("//div[@id='chapterlist']/li/a/text()").all();
            page.putField("urlList", listAll);
            page.putField("textList", listTextAll);
//            page.addTargetRequests(listAll);
            return;
        }
        String chapterTitle = page.getHtml().xpath("//div[@id='nr_title']/text()").toString();
        if (!StringUtils.isEmpty(chapterTitle)) {
            page.putField("pageType", "chapter");
            page.putField("chapterTitle", chapterTitle);
            String[] split = page.getUrl().toString().split("/");
            page.putField("articleId", split[split.length - 2]);
            page.putField("chapterId", split[split.length - 1].replace(".html", ""));

            List<String> ctList = page.getHtml().xpath("//div[@id='nr']/div[@id='nr1']/p/text()").all();
            StringBuilder sBuilder = new StringBuilder();
            for (String text : ctList) {
                sBuilder.append("    ").append(text).append("\n");
            }
            page.putField("content", sBuilder.toString());
        }

    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//        Spider.create(new SilukePageProcessor()).addPipeline(new SilukePipeline()).setDownloader(new HttpClientDownloader()).addUrl("https://m.siluke.com/0/110/110847/").thread(1).run();
    }

}
